//-----------------------------------------------------------------------
// <copyright file="tblOpicsTransactionDAOBase.cs" company="Scb">
//     Copyright ? Scb. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using Scb.Framework;
using Scb.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Robot.Entities;

namespace Robot.Data
{    
    public abstract class tblOpicsTransactionDAOBase
    {
        #region Common methods
        public virtual tblOpicsTransaction CreatetblOpicsTransactionFromReader(IDataReader reader)
        {
            tblOpicsTransaction item = new tblOpicsTransaction();
            try
            {
                if (UtilityDAO.CheckColumnData(reader,"ID")) item.ID = (Guid)reader["ID"];
                if (UtilityDAO.CheckColumnData(reader,"ValueDate")) item.ValueDate = (DateTime)reader["ValueDate"];
                if (UtilityDAO.CheckColumnData(reader,"ValueDateInOpics")) item.ValueDateInOpics = (DateTime)reader["ValueDateInOpics"];
                if (UtilityDAO.CheckColumnData(reader,"DealDate")) item.DealDate = (DateTime)reader["DealDate"];
                if (UtilityDAO.CheckColumnData(reader,"BuyCurrency")) item.BuyCurrency = (string)reader["BuyCurrency"];
                if (UtilityDAO.CheckColumnData(reader,"SellCurrency")) item.SellCurrency = (string)reader["SellCurrency"];
                if (UtilityDAO.CheckColumnData(reader,"CustomerRate")) item.CustomerRate = (decimal)reader["CustomerRate"];
                if (UtilityDAO.CheckColumnData(reader,"BuyAmount")) item.BuyAmount = (decimal)reader["BuyAmount"];
                if (UtilityDAO.CheckColumnData(reader,"SellAmount")) item.SellAmount = (decimal)reader["SellAmount"];
                if (UtilityDAO.CheckColumnData(reader,"InternalRate")) item.InternalRate = (decimal)reader["InternalRate"];
                if (UtilityDAO.CheckColumnData(reader,"Customer")) item.Customer = (string)reader["Customer"];
                if (UtilityDAO.CheckColumnData(reader,"Porfolio")) item.Porfolio = (string)reader["Porfolio"];
                if (UtilityDAO.CheckColumnData(reader,"Branch")) item.Branch = (string)reader["Branch"];
                if (UtilityDAO.CheckColumnData(reader,"ImportTransactionID")) item.ImportTransactionID = (Guid)reader["ImportTransactionID"];
                if (UtilityDAO.CheckColumnData(reader,"RunningDate")) item.RunningDate = (DateTime)reader["RunningDate"];
                if (UtilityDAO.CheckColumnData(reader,"Status")) item.Status = (int)reader["Status"];
                if (UtilityDAO.CheckColumnData(reader,"DealNo")) item.DealNo = (string)reader["DealNo"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreatetblOpicsTransactionFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreatetblOpicsTransaction methods
            
        public virtual void CreatetblOpicsTransaction(tblOpicsTransaction tblOpicsTransaction)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("sptblOpicsTransactionInsert");
                
                database.AddInParameter(dbCommand, "@ValueDate", DbType.DateTime, tblOpicsTransaction.ValueDate);
                database.AddInParameter(dbCommand, "@ValueDateInOpics", DbType.DateTime, tblOpicsTransaction.ValueDateInOpics);
                database.AddInParameter(dbCommand, "@DealDate", DbType.DateTime, tblOpicsTransaction.DealDate);
                database.AddInParameter(dbCommand, "@BuyCurrency", DbType.AnsiString, tblOpicsTransaction.BuyCurrency);
                database.AddInParameter(dbCommand, "@SellCurrency", DbType.AnsiString, tblOpicsTransaction.SellCurrency);
                database.AddInParameter(dbCommand, "@CustomerRate", DbType.Decimal, tblOpicsTransaction.CustomerRate);
                database.AddInParameter(dbCommand, "@BuyAmount", DbType.Decimal, tblOpicsTransaction.BuyAmount);
                database.AddInParameter(dbCommand, "@SellAmount", DbType.Decimal, tblOpicsTransaction.SellAmount);
                database.AddInParameter(dbCommand, "@InternalRate", DbType.Decimal, tblOpicsTransaction.InternalRate);
                database.AddInParameter(dbCommand, "@Customer", DbType.AnsiString, tblOpicsTransaction.Customer);
                database.AddInParameter(dbCommand, "@Porfolio", DbType.AnsiString, tblOpicsTransaction.Porfolio);
                database.AddInParameter(dbCommand, "@Branch", DbType.AnsiString, tblOpicsTransaction.Branch);
                database.AddInParameter(dbCommand, "@ImportTransactionID", DbType.Guid, tblOpicsTransaction.ImportTransactionID);
                database.AddInParameter(dbCommand, "@RunningDate", DbType.DateTime, tblOpicsTransaction.RunningDate);
                database.AddInParameter(dbCommand, "@Status", DbType.Int32, tblOpicsTransaction.Status);
                database.AddInParameter(dbCommand, "@DealNo", DbType.AnsiString, tblOpicsTransaction.DealNo);
                database.AddOutParameter(dbCommand, "@ID", DbType.Guid, 16);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                tblOpicsTransaction.ID = (Guid)database.GetParameterValue(dbCommand, "@ID");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreatetblOpicsTransactionException, ex);
            }
        }

        #endregion

        #region UpdatetblOpicsTransaction methods
        
        public virtual void UpdatetblOpicsTransaction(tblOpicsTransaction tblOpicsTransaction)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("sptblOpicsTransactionUpdate");            
                
                database.AddInParameter(dbCommand, "@ID", DbType.Guid, tblOpicsTransaction.ID);
                database.AddInParameter(dbCommand, "@ValueDate", DbType.DateTime, tblOpicsTransaction.ValueDate);
                database.AddInParameter(dbCommand, "@ValueDateInOpics", DbType.DateTime, tblOpicsTransaction.ValueDateInOpics);
                database.AddInParameter(dbCommand, "@DealDate", DbType.DateTime, tblOpicsTransaction.DealDate);
                database.AddInParameter(dbCommand, "@BuyCurrency", DbType.AnsiString, tblOpicsTransaction.BuyCurrency);
                database.AddInParameter(dbCommand, "@SellCurrency", DbType.AnsiString, tblOpicsTransaction.SellCurrency);
                database.AddInParameter(dbCommand, "@CustomerRate", DbType.Decimal, tblOpicsTransaction.CustomerRate);
                database.AddInParameter(dbCommand, "@BuyAmount", DbType.Decimal, tblOpicsTransaction.BuyAmount);
                database.AddInParameter(dbCommand, "@SellAmount", DbType.Decimal, tblOpicsTransaction.SellAmount);
                database.AddInParameter(dbCommand, "@InternalRate", DbType.Decimal, tblOpicsTransaction.InternalRate);
                database.AddInParameter(dbCommand, "@Customer", DbType.AnsiString, tblOpicsTransaction.Customer);
                database.AddInParameter(dbCommand, "@Porfolio", DbType.AnsiString, tblOpicsTransaction.Porfolio);
                database.AddInParameter(dbCommand, "@Branch", DbType.AnsiString, tblOpicsTransaction.Branch);
                database.AddInParameter(dbCommand, "@ImportTransactionID", DbType.Guid, tblOpicsTransaction.ImportTransactionID);
                database.AddInParameter(dbCommand, "@RunningDate", DbType.DateTime, tblOpicsTransaction.RunningDate);
                database.AddInParameter(dbCommand, "@Status", DbType.Int32, tblOpicsTransaction.Status);
                database.AddInParameter(dbCommand, "@DealNo", DbType.AnsiString, tblOpicsTransaction.DealNo);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdatetblOpicsTransactionException, ex);
            }
        }
        
        #endregion

        #region DeletetblOpicsTransaction methods
        public virtual void DeletetblOpicsTransaction(Guid id)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("sptblOpicsTransactionDelete");
                
                database.AddInParameter(dbCommand, "@ID", DbType.Guid, id);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeletetblOpicsTransactionException, ex);
            }
        }

        #endregion

        #region GettblOpicsTransaction methods

      public virtual tblOpicsTransaction GettblOpicsTransaction(Guid id)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("sptblOpicsTransactionGet");
                
                database.AddInParameter(dbCommand, "@ID", DbType.Guid, id);
                
                tblOpicsTransaction tblOpicsTransaction =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        tblOpicsTransaction = CreatetblOpicsTransactionFromReader(reader);
                        reader.Close();
                    }
                }
                return tblOpicsTransaction;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGettblOpicsTransactionException, ex);
            }
        }
        
        #endregion

        #region GettblOpicsTransactionList methods
        public virtual tblOpicsTransactionCollection GettblOpicsTransactionList(tblOpicsTransactionColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("sptblOpicsTransactionGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                tblOpicsTransactionCollection tblOpicsTransactionCollection = new tblOpicsTransactionCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        tblOpicsTransaction tblOpicsTransaction = CreatetblOpicsTransactionFromReader(reader);
                        tblOpicsTransactionCollection.Add(tblOpicsTransaction);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return tblOpicsTransactionCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGettblOpicsTransactionListException, ex);
            }
        }
        
        public virtual tblOpicsTransactionCollection GettblOpicsTransactionList(tblOpicsTransactionColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GettblOpicsTransactionList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}